#include "aky_program/aky_com_include.h"
typedef enum
{
    set_main = 0,
    set_voice,
    set_bl,
    set_network,
    set_max,
}e_set_stat_t;

static void my_event_cb(lv_obj_t * obj, lv_event_t event);
void set_stat_pro(void);

static lv_obj_t *tc_area_back;          //
static lv_obj_t *tc_area_voice;          //声音
static lv_obj_t *tc_area_bl;          //屏幕亮度
static lv_obj_t *tc_area_network;          //网络设置
static lv_obj_t *level_img_sets;          //
static lv_obj_t *slider_sets;          //
e_set_stat_t e_set_stat = set_main;
static lv_obj_t *sw_network;          //

/*
 * 界面回调函数
 */
static void screen006_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    screen_flush_pro();
    set_stat_pro();
    if (get_spk_recogni_info() == SPK_BACK)
    {
        clr_spk_recogni_info();
        store_set_to_flash(g_s_set);
        lv_levelimg_set_level(level_img_sets, 0);
        lv_obj_set_hidden(slider_sets, true);
        e_set_stat = set_main;
        synwit_ui_load_screen(SCREEN005);      
    }
    if (get_spk_recogni_info() == VOICE_SET)
    {
        clr_spk_recogni_info();
        e_set_stat = set_voice;
        lv_event_send(tc_area_voice,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == BL_SET)
    {
        clr_spk_recogni_info();
        e_set_stat = set_bl;
        lv_event_send(tc_area_bl,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == BL_UP || get_spk_recogni_info() == BL_DOWN)
    {
        clr_spk_recogni_info();
        if (e_set_stat == set_bl)
        {
            lv_slider_set_value(slider_sets, g_s_set.bl_level * BL_LEVEL_STEP_VAL, LV_ANIM_OFF);
        }
    }
    if (get_spk_recogni_info() == VOICE_UP || get_spk_recogni_info() == VOICE_DOWN || get_spk_recogni_info() == VOICE_SHUT)
    {
        clr_spk_recogni_info();
        if (e_set_stat == set_voice)
        {
            lv_slider_set_value(slider_sets, g_s_set.voice_level * VOICE_LEVEL_STEP_VAL, LV_ANIM_OFF);
        }
    }
    if (get_spk_recogni_info() == NET_SET)
    {
        clr_spk_recogni_info();
        e_set_stat = set_network;
        lv_event_send(tc_area_network,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == NET_ON)
    {
        clr_spk_recogni_info();
        if (e_set_stat == set_network)
        {
            g_s_set.flag_net_work = 1;
            lv_switch_on(sw_network,LV_ANIM_OFF);
        }
        lv_event_send(tc_area_network,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == NET_OFF)
    {
        clr_spk_recogni_info();
        if (e_set_stat == set_network)
        {
            g_s_set.flag_net_work = 0;
            lv_switch_off(sw_network,LV_ANIM_OFF);
        }
        lv_event_send(tc_area_network,LV_EVENT_CLICKED,NULL);
    }

}

static void screen006_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    e_set_stat = set_main;
    tc_area_back = synwit_ui_find_lv_obj(WIDGET_TOUCH_15);
    tc_area_voice = synwit_ui_find_lv_obj(WIDGET_TOUCH_12);
    tc_area_bl = synwit_ui_find_lv_obj(WIDGET_TOUCH_13);
    tc_area_network = synwit_ui_find_lv_obj(WIDGET_TOUCH_14);

    level_img_sets = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_1);
    slider_sets = synwit_ui_find_lv_obj(WIDGET_SLIDER_1);
    sw_network = synwit_ui_find_lv_obj(WIDGET_SW2);

    lv_obj_set_hidden(slider_sets, true);
    lv_obj_set_hidden(sw_network, true);
    if (g_s_set.flag_net_work == 1)
    {
        lv_switch_on(sw_network,LV_ANIM_OFF);
    }
    else
    {
        lv_switch_off(sw_network,LV_ANIM_OFF);
    }

    lv_obj_set_event_cb(slider_sets, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_voice, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_bl, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_network, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(sw_network, my_event_cb);   /*Assign an event callback*/


    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen006_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
}

static int screen006_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen006_cb_obj = {
    .favorite_stop_policy = screen006_favorite_stop_policy,
    .on_screen_start = screen006_start,
    .on_screen_stop = screen006_stop,
    .on_screen_timer = screen006_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    uint16_t slider_val;
    uint8_t bl_level;
    uint8_t voice_level;
    off_time_clr();
    if (e_set_stat == set_main)
    {
        if (obj == tc_area_voice)
        {
            e_set_stat = set_voice;
        }
        if (obj == tc_area_bl)
        {
            e_set_stat = set_bl;
        }
        if (obj == tc_area_network)
        {
            e_set_stat = set_network;
        }
        if (obj == tc_area_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                store_set_to_flash(g_s_set);
                synwit_ui_load_screen(SCREEN005);
            }
        }
    }
    else if (e_set_stat == set_voice)
    {
        if (obj == tc_area_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_set_stat = set_main;
            }
        }
        if (obj == slider_sets)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                slider_val = lv_slider_get_value(slider_sets);
                printf("slider_val:%d\n", slider_val);
                voice_level = get_voice_level(slider_val);
                printf("voice_level:%d\n", voice_level);
                g_s_set.voice_level = voice_level;
            }
        }
    }
    else if (e_set_stat == set_bl)
    {
        if (obj == tc_area_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_set_stat = set_main;
            }
        }
        if (obj == slider_sets)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                slider_val = lv_slider_get_value(slider_sets);
                printf("slider_val:%d\n", slider_val);
                bl_level = get_bl_level(slider_val);
                printf("bl_level:%d\n", bl_level);
                set_bl_by_level(bl_level);
                g_s_set.bl_level = bl_level;
            }
        }
    }
    else if (e_set_stat == set_network)
    {
        if (obj == tc_area_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_set_stat = set_main;
            }
        }
        if (obj == sw_network)
        {
            if (event == LV_EVENT_CLICKED)
            {
                bool sw_stat = lv_switch_get_state(sw_network);
                g_s_set.flag_net_work = sw_stat;
            }
        }
    }



}
void set_stat_pro(void)
{
    static e_set_stat_t e_set_stat_last = set_max;
    static uint16_t step = 0;
    if (e_set_stat_last != e_set_stat)
    {
        e_set_stat_last = e_set_stat;
        step = 0;
    }
    

    switch (e_set_stat)
    {
    case set_main:
    {
        if (step == 0)
        {
            step++;
            lv_levelimg_set_level(level_img_sets, 0);
            lv_obj_set_hidden(slider_sets, true);
            lv_obj_set_hidden(sw_network, true);
        }
        else if (step == 1)
        {
            /* code */
        }
        
        break;
    }
    case set_voice:
    {
        if (step == 0)
        {
            step++;
            lv_levelimg_set_level(level_img_sets, 3);
            lv_obj_set_hidden(slider_sets, false);
            lv_obj_set_hidden(sw_network, true);
            lv_slider_set_value(slider_sets, g_s_set.voice_level * VOICE_LEVEL_STEP_VAL, LV_ANIM_OFF);
        }
        else if (step == 1)
        {
            /* code */
        }
        
        break;
    }   
    case set_bl:
    {
        if (step == 0)
        {
            step++;
            lv_levelimg_set_level(level_img_sets, 2);
            lv_obj_set_hidden(slider_sets, false);
            lv_obj_set_hidden(sw_network, true);
            lv_slider_set_value(slider_sets, g_s_set.bl_level * BL_LEVEL_STEP_VAL, LV_ANIM_OFF);
        }
        else if (step == 1)
        {
            /* code */
        }
        
        break;
    } 
    case set_network:
    {
        if (step == 0)
        {
            step++;
            lv_levelimg_set_level(level_img_sets, 1);
            lv_obj_set_hidden(slider_sets, true);
            lv_obj_set_hidden(sw_network, false);
        }
        else if (step == 1)
        {
            /* code */
        }
        
        break;
    } 
    default:
        break;
    }
}